export const formatTime = (date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return (
    [year, month, day].map(formatNumber).join('/') +
    ' ' +
    [hour, minute, second].map(formatNumber).join(':')
  )
}

const formatNumber = (n) => {
  const s = n.toString()
  return s[1] ? s : '0' + s
}

export const rpx2px = (rpx, systeminfo) => {
  let screenWidth = 0;
  if (systeminfo) {
    screenWidth = systeminfo.screenWidth;
  }
  else {
    screenWidth = wx.getSystemInfoSync().screenWidth
  }
  return (screenWidth * Number.parseInt(rpx)) / 750
}
/**
 * 判断URL是否为http
 * @param url 
 * @returns 
 */

export const flagHttpUrl = (url) => {
  if (url.startsWith('http://') || url.startsWith('http://')) {
    return true;
  }
  return false;
}

/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string | null}
 */
export function parseTime(time, cFormat) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string')) {
      if ((/^[0-9]+$/.test(time))) {
        // support "1548221490638"
        time = parseInt(time)
      } else {
        // support safari
        // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
        time = time.replace(new RegExp(/-/gm), '/')
      }
    }

    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
    return value.toString().padStart(2, '0')
  })
  return time_str
}


/**
 * 获取当前页面路径
 */
export const getCurrentPagePath=()=> {
  // 使用uni-app的方法获取当前页面栈
  const pages = getCurrentPages();
  // 获取当前页面的路径
  const currentPage = pages[pages.length - 1];
  const pagePath = currentPage.route;
  // 打印当前页面路径
  // console.log('当前页面路径：', pagePath);
  return pagePath
}
